package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.AbstractCustomDialogFragment;
import com.wgts.wgts.control.dialog.PlatformPayData;
import com.wgts.wgts.control.editor.PasswordEditText;
import com.wgts.wgts.mvp.view.activity.setPassWord.SettingPasswordActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PutForwardFragmentDialog extends AbstractCustomDialogFragment {
    private static String PlatformPayDialogFragmentBundle = "PlatformPayDialogFragmentBundle";
    @BindView(R.id.need_pay_price)
    TextView needPayPrice;
    @BindView(R.id.residue_coin_text)
    TextView residueCoinText;
    @BindView(R.id.password_edit)
    PasswordEditText passwordEdit;
    @BindView(R.id.forget_password_ly)
    LinearLayout forgetPasswordLy;
    @BindView(R.id.cancel_dialog)
    ImageView cancelDialog;
    Unbinder unbinder;
    private PlatformPayData data;

    private InputOverListener inputOverListener;

    public void setInputOverListener(InputOverListener inputOverListener) {
        this.inputOverListener = inputOverListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Override
    public View onCreateDialogView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            data = bundle.getParcelable(PlatformPayDialogFragmentBundle);
        }
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_fragment_put_forward, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    public static PutForwardFragmentDialog getPlatformPayDialogFragment(PlatformPayData platformPayData) {
        PutForwardFragmentDialog dialogFragment = new PutForwardFragmentDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelable(PlatformPayDialogFragmentBundle, platformPayData);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    private void initView(){
        needPayPrice.setText(data.getPayPrice());
        residueCoinText.setText(data.getResidueCoin());
        showSoftInputFromWindow(passwordEdit);
        checkPassword();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume(){
        super.onResume();
        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inManager = (InputMethodManager)passwordEdit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 100);
    }

    @OnClick({R.id.forget_password_ly, R.id.cancel_dialog})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forget_password_ly:
                forgetPassword();
                break;
            case R.id.cancel_dialog:
                dismiss();
                break;
        }
    }

    //忘记密码跳转
    private void forgetPassword(){
        Intent intent = new Intent(getContext(), SettingPasswordActivity.class);
        startActivity(intent);
        dismiss();
    }

    //输入密码完成进行支付
    private void checkPassword(){
        passwordEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() >= 6){
                    if(inputOverListener != null){
                        inputOverListener.inputOver(s.toString());
                    }
                    pay();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    //密码判断及支付方法
    private void pay(){
        dismiss();
        hideKeyboard(passwordEdit);
    }
    /**
     * EditText获取焦点
     */
    public void showSoftInputFromWindow(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }
    //隐藏软键盘
    public void hideKeyboard(View view){
        InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService
                (Context.INPUT_METHOD_SERVICE);
        if (imm != null){
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    public interface InputOverListener{
        void inputOver(String s);
    }
}

